package com.company.mp;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.SeekBar;

public class MainActivity extends AppCompatActivity {

    private SeekBar skb;
    private Intent intent;
    private int pro;
    private musicservice.myBinder ms;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        skb = (SeekBar)findViewById(R.id.skb);
        handler =new Handler(){
            @Override
            public void handleMessage(Message msg) {
                skb.setProgress(msg.getData().getInt("pro"));
            }
        };
        intent = new Intent("mymusic");
        bindService(intent, new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                ms = (musicservice.myBinder) service;
                thread.start();

            }
            @Override
            public void onServiceDisconnected(ComponentName name) {
            }
        }, BIND_AUTO_CREATE);
    }

    public void play(View view) {
        intent.putExtra("action", 1);
        startService(intent);
    }
    public void pause(View view) {
        intent.putExtra("action",2);
        startService(intent);
    }
    public void resume(View view) {
        intent.putExtra("action", 3);
        startService(intent);
    }

    Thread thread = new Thread(){
        @Override
        public void run() {
            for(int i=0;i<400;i++){
                try {
                    Thread.sleep(30);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                pro = ms.getProgress();
                Log.i("pro",""+pro);
                Message msg = new Message();
                Bundle bundle = new Bundle();
                bundle.putInt("pro", pro);
                msg.setData(bundle);
                handler.sendMessage(msg);
            }
        }
    };
}
